<?php

namespace Frieza\Modules\Topic\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\tools\Log;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Doraemon\cache\HotValue;
use Doraemon\model\TopicActivity;
use Doraemon\cache\TopicCounter;
use Doraemon\model\Topic\TopicHot;
use Doraemon\model\Topic\TopicActivity as mTopicActivity;

class RankTopicListController extends Controller
{

    public function info(Content $content, Request $request)
    {

        $rankId = $request['rank_id'];
        $page = $request->input('page', 1);
        $pageSize = $request->input('per_page', 20);
        $topicId = $request->input('topic_id');
        $title = $request->input('title');
        $count = HotValue::getCountRankList($rankId);
//        $start = ($page - 1) * $pageSize;
//        $end = $page * $pageSize - 1;
        $no = null;
        $topicHotInfo = TopicHot::getInfo(['id' => $rankId]);
        $topicIds = explode(',', $topicHotInfo['topic_id']);
        $topicIdsAll = $topicIds;

        if ($title) {
            $topicInfo = mTopicActivity::getDetailByKeyword($title);
            $searchId = $topicInfo['id'] ?? 0;
            $topicIds = [$searchId];
            $no = array_search($searchId, $topicIdsAll) + 1;
        }
        if ($topicId) {
            $topicIds = [$topicId];
            $no = array_search($topicId, $topicIdsAll) + 1;
        }

        $topics = mTopicActivity::getByIdsNotDel($topicIds);
        //dd($topics);
        $topicsInfo = [];
        foreach ($topics as $topic) {
            $topicsInfo[$topic['id']] = $topic;
        }
        $rankList = [];
        foreach ($topicIds as $key => $topicId) {
            $rank = $no ? $no : ($page - 1) * $pageSize + $key + 1;
            $rankList[$key]['no'] = 'NO.' . $rank;
            $rankList[$key]['id'] = $topicId;
            $rankList[$key]['topic_id'] = $topicId;
            $rankList[$key]['title'] = $topicsInfo[$topicId]['title'];
        }
        $gridModel = new pGridDataModel($rankList, $count, [
            'perPage' => $pageSize,
        ]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->expandFilter();
            $grid->disablePagination();
            $grid->disableFilter();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('topic_id', '话题ID')->placeholder('话题ID');
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('title', '话题名称')->placeholder('话题名称');
                });
            });

            $grid->column('no', '当前排名');
            $grid->column('title', '话题名称');
            $grid->column('topic_id', '话题ID');
//            $grid->column('answer', '总回答数');
//            $grid->column('follow', '总关注数');
//            $grid->column('joiner', '回答总交互数');
//            $grid->column('todayJoiner', '24h回答新增交互');
//            $grid->column('todayAnswer', '24h新增回答数');
//            $grid->column('todayFollow', '24h新增关注数');
//            $grid->column('score', '当前热度值');
//            $grid->column('manHot', '人工热度值')->editable();
//            $grid->column('machineHot', '机器热度值');

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                // 去掉编辑
                $actions->disableEdit();
                // 去掉查看
                $actions->disableView();
            });
        });
        $title = TopicHot::getInfo(['id' => $rankId],'title' );
        return $content
            ->header($title)
            ->breadcrumb(['text' => $title, 'url' => '/admin/topic/hot/index'])
            ->body($listGrid);
    }

    public function changehot($rankId, $topicId, Request $request)
    {
        try {
            $score = $request->input('value', '');
            if (!preg_match("/^[1-9][0-9]*$/", $score)) {
                return response()->json([
                    'status' => false,
                    'message' => "请输入正整数",
                ]);
            }
            $oldScore = HotValue::getManHot($rankId, $topicId);
            $addScore = $score - $oldScore;
            HotValue::setManHot($rankId, $topicId, $score);
            HotValue::addScore($rankId, $topicId, $addScore);
            return response()->json([
                'status' => true,
                'message' => "操作成功",
            ]);
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
    }
}